package com.example.finaltool.utils;

import com.example.finaltool.consts.Global;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author dengbw
 * @date 2021/9/28 10:13 上午
 */
@Slf4j
public class CommandUtils {

    private CommandUtils() {
    }

    public static String exec(String command) {
        log.info(command);
        Process p;
        try {
            //执行命令
            p = Runtime.getRuntime().exec(command);
            //获取输出流，并包装到BufferedReader中
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line = br.readLine()) != null) {
                builder.append(line).append(System.lineSeparator());
                log.info(line);
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String buildDataTransferCommand(String txtPath, String excelPath) {
        return Global.DATA_TRANSFER_HOME + " -d -s " + excelPath + " -f " + txtPath;
    }

    public static String buildUserTransferCommand(String txtPath, String excelPath) {
        return Global.DATA_TRANSFER_HOME + " -u -s " + excelPath + " -f " + txtPath;
    }
}
